//
//  CTAlertView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/30.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTAlertView.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "UIViewAdditions.h"
#import "CTAppLanguage.h"
#import "CTRemindView.h"
#import "CTTextManager.h"

@interface CTAlertView ()<UITextFieldDelegate>
{
    UIImageView *chooseImgeView;
    UIView *backGroundView;
    UIView *foreGroundView;
    NSString *textFieldStr;
    UITextField *alertTextField;
    CTAlertViewTagType tagType;
    UILabel *titleLabel;
}
@end


@implementation CTAlertView
@synthesize delegate;


- (void)dealloc
{
    chooseImgeView = nil;
    backGroundView = nil;
    foreGroundView = nil;
    textFieldStr   = nil;
    alertTextField = nil;
    titleLabel     = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTAlertView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
    
        self.frame  = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
        [self createAlertView];
    }
    return self;
}

- (void)showAlertViewOn:(UIView *)view withType:(CTAlertViewType)type title:(NSString *)titleName delegate:(id)dele
{
    if (view ==nil)
    {
        return;
    }
    [view addSubview:self];
    if (dele != nil)
    {
        self.delegate = dele;
    }

    titleLabel.text = titleName;
    //是否需要区分文件类型
    if (type ==CTAlertViewTypeDefault)
    {
        tagType = CTAlertViewTagTypeEmoji;
        
        chooseImgeView = [[UIImageView alloc]init];
        chooseImgeView.image = [UIImage imageNamed:@"add_radio_choose"];
        NSArray *imageArray = [NSArray arrayWithObjects:@"add_radio",@"add_radio", nil];
        NSArray *typeArray = [NSArray arrayWithObjects:[CTAppLanguage localizedStringForKey:@"alertView_typeName1" withValue:nil withTable:@"Language"],[CTAppLanguage localizedStringForKey:@"alertView_typeName2" withValue:nil withTable:@"Language"], nil];
        for (int i  = 0; i < 2; i ++)
        {
            UIButton *typeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            typeBtn.frame = CGRectMake(41+i*105, alertTextField.bottom+21, 23, 23);
            [typeBtn setBackgroundImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
            typeBtn.tag = 1000+i;
            [typeBtn addTarget:self action:@selector(typeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [foreGroundView addSubview:typeBtn];
            if (i == 0)
            {
                chooseImgeView.frame = typeBtn.frame;
            }
            UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(typeBtn.right+10, typeBtn.origin.y, 97, 23)];
            nameLabel.textColor = [UIColor colorWithRed:255/255.0 green:132/255.0 blue:49/255.0 alpha:1.0];
            nameLabel.font = [UIFont fontWithName:nil size:15];
            nameLabel.textAlignment = NSTextAlignmentLeft;
            nameLabel.text = typeArray[i];
            [foreGroundView addSubview:nameLabel];
        }
        [foreGroundView addSubview:chooseImgeView];
    }
    else
    {
        tagType = CTAlertViewTagTypeNull;
        
        foreGroundView.frame = CGRectMake(0, 0, 270, 207-45);
        foreGroundView.center = self.center;
    }


    
}
- (void)createAlertView
{
    backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT)];
    //    backGroundView.userInteractionEnabled = YES;
    backGroundView.backgroundColor = [UIColor blackColor];
    backGroundView.alpha = 0.8;
    [self addSubview:backGroundView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [backGroundView addGestureRecognizer:tap];
    
    foreGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 270, 207)];
    foreGroundView.backgroundColor = CT_HEADER_WHITE_COLOR;
    foreGroundView.center = self.center;
    foreGroundView.layer.masksToBounds = YES;
    foreGroundView.layer.cornerRadius  = 5;
    [self addSubview:foreGroundView];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [foreGroundView addGestureRecognizer:tap1];
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, foreGroundView.width, 18)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = CT_HEADER_BLACK_COLOR;
    titleLabel.font = [UIFont fontWithName:nil size:18];
    [foreGroundView addSubview:titleLabel];
    
    UIImageView *textFieldImage = [[UIImageView alloc]initWithFrame:CGRectMake(17, titleLabel.bottom+25, foreGroundView.width-34, 30)];
    textFieldImage.image = [UIImage imageNamed:@"add_shurukuang"];
    [foreGroundView addSubview:textFieldImage];
    alertTextField = [[UITextField alloc]initWithFrame:CGRectMake(17+5, titleLabel.bottom+25, foreGroundView.width-34-10, 30)];
    alertTextField.delegate = self;
    alertTextField.font = [UIFont fontWithName:nil size:13];
    alertTextField.placeholder = [CTAppLanguage localizedStringForKey:@"alertView_textField_placeholder" withValue:nil withTable:@"Language"];
    alertTextField.textAlignment = NSTextAlignmentLeft;
    alertTextField.textColor = CT_HEADER_BRIGHTLINE_COLOR;
    [foreGroundView addSubview:alertTextField];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, foreGroundView.height-51, foreGroundView.width/2,51);
    [cancelBtn addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitle:[CTAppLanguage localizedStringForKey:@"alertView_btnTitle_cancel" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithRed:255/255.0 green:132/255.0 blue:49/255.0 alpha:1.0] forState:UIControlStateNormal];
    [foreGroundView addSubview:cancelBtn];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(cancelBtn.right, foreGroundView.height-51, foreGroundView.width/2,51);
    [sureBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitle:[CTAppLanguage localizedStringForKey:@"alertView_btnTitle_sure" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor colorWithRed:49/255.0 green:93/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
    [foreGroundView addSubview:sureBtn];
    
    UILabel *bottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, foreGroundView.height-51, foreGroundView.width, 0.5)];
    bottomLine.backgroundColor =[UIColor colorWithRed:207/255.0 green:207/255.0 blue:207/255.0 alpha:1.0];
    [foreGroundView addSubview:bottomLine];
    
    UILabel *verticalLabel =[[UILabel alloc]initWithFrame:CGRectMake(foreGroundView.width/2, bottomLine.bottom, 0.5, 51)];
    verticalLabel.backgroundColor = [UIColor colorWithRed:207/255.0 green:207/255.0 blue:207/255.0 alpha:1.0];
    [foreGroundView addSubview:verticalLabel];
}
#pragma mark TextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    textFieldStr = textField.text;
}
//选择类别
- (void)typeBtnAction:(UIButton *) sender
{
    chooseImgeView.frame = sender.frame;
    switch (sender.tag) {
        case 1000:
            tagType = CTAlertViewTagTypeEmoji;
            break;
        case 1001:
            tagType = CTAlertViewTagTypePic;
            break;
            
        default:
            break;
    }
    [alertTextField resignFirstResponder];
}
//取消
- (void)cancelBtnAction:(UIButton *) sender
{
    [self removeAlertView];
}
//确定
- (void)sureBtnAction:(UIButton *) sender
{
    [alertTextField resignFirstResponder];
    CTTextManager *textManager = [[CTTextManager alloc]init];
    CTRemindView *remidView = [[CTRemindView alloc]init];
    if ([textManager isBlankString:textFieldStr])
    {
        [remidView showRemindViewOn:self withRemindWord:[CTAppLanguage localizedStringForKey:@"alertView_fileName" withValue:nil withTable:@"Language"]];
        return;
    }
    if ([textManager isFoundString:@" " withStr:textFieldStr])
    {
        [remidView showRemindViewOn:self withRemindWord:[CTAppLanguage localizedStringForKey:@"alertView_fileName_space" withValue:nil withTable:@"Language"]];
        return;
    }
    if ([self.delegate respondsToSelector:@selector(alertViewClickedForConfirm:withTextFieldText:withTagType:)])
    {
        [self.delegate alertViewClickedForConfirm:self withTextFieldText:textFieldStr withTagType:tagType];
    }
    [self removeAlertView];
}
-(void)removeAlertView
{
    [backGroundView removeFromSuperview];
    [foreGroundView removeFromSuperview];
    [chooseImgeView removeFromSuperview];
    [self removeFromSuperview];
}
//收回键盘
- (void)tapAction:(UITapGestureRecognizer *) tap
{
    [alertTextField resignFirstResponder];
}
@end
